<?php

namespace App\Http\Controllers\Admin\Manager;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Http\Requests\Admin\StorePermissionPostRequest;

use App\Models\Admin\Permission;

class PermissionController extends Controller
{
    public $module = 'permission_tag';

    public $parent_module = 'rbac';

    public $breadcrumb_name = 'permission';

    public $page_name = 'RBAC';

    public $page_describe = '权限管理';

    public function index()
    {
        $permissions = Permission::paginate(25);
        return view('manager.permission.index', compact('permissions'));
    }

    public function create()
    {
        return view('manager.permission.create');
    }

    public function store(StorePermissionPostRequest $request)
    {
        try {
            if (Permission::create($request->all())) {
                return redirect()->route('permission.index')->withSuccess('新增权限成功');
            }
        } catch (\Exception $e) {
            return redirect()->back()->withErrors(array('error' => $e->getMessage()))->withInput();
        }
    }

    public function show($id)
    {
        dd('不支持预览！');
    }

    public function edit($id)
    {
        $permission = Permission::find($id);
        return view('manager.permission.edit', compact('permission'));
    }

    public function update(Request $request, $id)
    {
        $data = $request->all();
        unset($data['_token']);
        unset($data['_method']);
        try {
            if (Permission::where('id', $id)->update($data)) {
                return redirect()->back()->withSuccess('编辑角色成功');
            }
        } catch (\Exception $e) {
            return redirect()->back()->withErrors(array('error' => $e->getMessage()))->withInput();
        }
    }

    public function destroy($id)
    {
        try {
            $permission = Permission::find($id);
            if ($permission->delete()) {
                return redirect()->back()->withSuccess('删除权限成功');
            }
        } catch(\Exception $e) {
            return redirect()->back()->withErrors(array('error' => $e->getMessage()));
        }
    }
}
